﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Models.Products;
using yiCommerce.Service.Pictures;
using yiCommerce.Service.Products;
using yiCommerce.Service.ShoppingCarts;

namespace yiCommerce.Controllers
{
    public class ProductController : Controller
    {
        private readonly IShoppingCartService _shoppingCartService;
        private readonly IProductService _productService;
        private readonly IWorkContext _workContext;
        private IPictureService _pictureService;
        private IProductSearchService _productSearchService;

        public ProductController(
            IProductSearchService productSearchService,
            IShoppingCartService shoppingCartService, IProductService productService, IWorkContext workContext, IPictureService pictureService)
        {
            _productSearchService = productSearchService;
            _shoppingCartService = shoppingCartService;
            _productService = productService;
            _workContext = workContext;
            _pictureService = pictureService;
        }

        public ActionResult NewProducts()
        {
            var products = _productSearchService.SearchProducts(0, 0, 0, string.Empty, ProductSortingEnum.CreatedOn, 0,30);
            var pModels = yiCommerce.Mapper.ProductMapper.PrepareProductOverviewModels(products);
            ViewBag.Model = pModels;
            return View();
        }

        public ActionResult Product(int productId)
        {

            var product = _productService.GetProductById(productId);
            if (product == null || product.Deleted || !product.Published)
                return Redirect("/");

            var model = PrepareProductDetailsPageModel(product);

            ViewBag.Model = model;
            return View("SingleProduct");
        }





        protected ProductDetailsModel PrepareProductDetailsPageModel(Product product)
        {

            if (product == null)
                throw new ArgumentNullException("product");

            var model = new ProductDetailsModel()
            {
                Id = product.Id,
                Name = product.ProductName,
                ShortDescription = product.ShortDescription,
                FullDescription = product.FullDescription,
               
                PictureModels = new List<PictureModel>(),

                ProductVariantModels = new List<ProductDetailsModel.ProductVariantModel>(),
                SpecificationAttributeModels= new List<ProductSpecificationAttributeModel>(),

             };

            var productPictures = product.ProductPictures;

            if (productPictures.Count > 0)
            {
                //default picture
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl = _pictureService.GetPictureUrl(productPictures.FirstOrDefault().Picture.Id, 300,false),
                    FullSizeImageUrl = _pictureService.GetPictureUrl(productPictures.FirstOrDefault().Picture.Id,0, false),
                    Title =  model.Name,
                };
                //all pictures
                foreach (var productPicture in productPictures)
                {
                    model.PictureModels.Add(new PictureModel()
                    {
                        ImageUrl = _pictureService.GetPictureUrl(productPicture.Picture.Id, 300,false),
                        FullSizeImageUrl = _pictureService.GetPictureUrl(productPicture.Picture.Id,0,false),
                        Title = model.Name,
                    });
                }
            }
            else
            {
                //no images. set the default one
                model.DefaultPictureModel = new PictureModel()
                {
                    ImageUrl = _pictureService.GetDefaultPictureUrl(300),
                    FullSizeImageUrl = _pictureService.GetDefaultPictureUrl(0),
                    Title =  model.Name,
                };
            }

            //product variants
            foreach (var variant in product.ProductVariants)
            {

                var pvm = PrepareProductVariantModel(variant);
                model.ProductVariantModels.Add(pvm);
            }

            foreach (var attribute in product.ProductSpecificationAttributes)
            {
                var m = PrepareProductSpecificationAttributeModel(attribute);
                model.SpecificationAttributeModels.Add(m);
            }

            return model;
        }

        private ProductSpecificationAttributeModel PrepareProductSpecificationAttributeModel(ProductSpecificationAttribute attribute)
        {
            var model = new ProductSpecificationAttributeModel()
                {
                    AttributeName = attribute.SpecificationAttributeOption.SpecificationAttribute.Name,
                    AttributeOptionName = attribute.SpecificationAttributeOption.Name,
                };
            return model;
        }

        protected ProductDetailsModel.ProductVariantModel PrepareProductVariantModel(
            ProductVariant productVariant)
        {
            if (productVariant == null)
                throw new ArgumentNullException("productVariant");

            var model = new ProductDetailsModel.ProductVariantModel();
            model.Id = productVariant.Id;
            model.Name = productVariant.Name;
            model.Sku = productVariant.Sku;
            model.Description = productVariant.Description;
            model.StockQuantity = productVariant.StockQuantity;


            model.PictureModel = new PictureModel();
            model.PictureModel.FullSizeImageUrl = _pictureService.GetPictureUrl(productVariant.PictureId, 0, false);
            model.PictureModel.ImageUrl = _pictureService.GetPictureUrl(productVariant.PictureId,300, false);
            model.PictureModel.Title = productVariant.Name;

            model.Price = productVariant.Price;

            model.AddToCart = new AddToCartModel();
            model.AddToCart.ProductVariantId = productVariant.Id;
            model.ProductVariantAttributes = new List<ProductDetailsModel.ProductVariantModel.ProductVariantAttributeModel>();

            var productVariantAttributes = productVariant.ProductVariantAttributes;
            foreach (var attribute in productVariantAttributes)
            {
                var pvaModel = new ProductDetailsModel.ProductVariantModel.ProductVariantAttributeModel()
                {
                    Id = attribute.Id,
                    ProductVariantId = productVariant.Id,
                    ProductAttributeId = attribute.ProductAttributeId,
                    Name = attribute.ProductAttribute.Name,
                    Description = attribute.ProductAttribute.Description,
                   
                    AttributeControlType = attribute.AttributeControlType,
                    ProductVariantAttributeValueModels = attribute.ProductVariantAttributeValues==null? new List<ProductVariantAttributeValueModel>() : 
                    attribute.ProductVariantAttributeValues.Select(c=>new ProductVariantAttributeValueModel()
                        {
                            Id = c.Id,
                            Name = c.Name,
                            DisplayOrder = c.DisplayOrder
                            
                        }).ToList(),
                };
                model.ProductVariantAttributes.Add(pvaModel);
            }
            return model;
        }


        //public JsonResult AddProductVariantToCart(int productVariantId, int quantity, IList<ProductVariantAttributeValue> attributeValues)
        //{

        //    var pv = _productService.GetProductVariantById(productVariantId);

        //    var selectedAttributes = string.Empty;

        //    _shoppingCartService.AddToCart(_workContext.CurrentCustomer.Id, pv, selectedAttributes, quantity);

        //    return null;
            
        //}
    }
}
